package org.example.study.sc.rabbitmq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.example.study.sc.dto.SecKillMsg;
import org.example.study.sc.rabbitmq.config.RabbitConfig;
import org.example.study.sc.service.OrderMasterService;
import org.example.study.sc.service.SecKillService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @program: rabbitmq-study
 * @description:
 * @Version V1.0.0
 * @author: dazhi
 * @create: 2021-01-28 10:49
 **/
@Slf4j
@Component
public class OrderReceiver {
    @Autowired
    private SecKillService secKillService;

    @Autowired
    private OrderMasterService orderMasterService;

    @RabbitListener(queues = RabbitConfig.QUEUE_ORDER_SECKILL)
    public void receiveSecKillMsg(SecKillMsg secKillMsg, Message message, Channel channel) throws IOException {
        log.info("TAG,{}", String.valueOf(message.getMessageProperties().getDeliveryTag()));
        //执行秒杀请求
        secKillService.secKill(secKillMsg.getGoodsNo(), secKillMsg.getMemberId());
        //channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }


    /**
     * 超时订单自动取消消息监听
     * @param orderId
     * @param msg
     */
    @RabbitListener(queues = RabbitConfig.QUEUE_ORDER_CANCEL)
    public void receiveOrderCancelMsg(Long orderId, Message msg,Channel channel)  throws IOException {
        log.info("超时订单取消:{}",orderId);
        // 调用超时订单取消业务
        orderMasterService.orderCancel(orderId);
        // 手动消息确认
       // channel.basicAck(msg.getMessageProperties().getDeliveryTag(),true);

    }
}
